package com.erb.controller.hzg;

import com.erb.constants.LoanConstants;
import com.erb.entity.LoanUser;
import com.erb.service.hzg.loanLoginService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Controller
@RequestMapping("loan/reglog")
public class loanreglog {
	int codes = 0;
	@Resource
	loanLoginService service;

	@RequestMapping("toreg")
	public String toreg() {
		return "loan/reglog/loanregister";
	}

	@RequestMapping("tologin")
	public String tologin() {
		return "loan/reglog/loanLogin";
	}

	//注册输入框验证
	@RequestMapping("/seluser")
	@ResponseBody
	public Map seluser(LoanUser user) {
		Map map = new HashMap();
		LoanUser user1 = service.seluser(user);
		if (user1 != null) {  //用户名或号码存在时
			map.put("msg", 1);
		} else {
			map.put("msg", 0);
		}
		return map;
	}

	//验证用户名是否存在
	@RequestMapping("/selusers")
	@ResponseBody
	public Map selusers(LoanUser user) {
		Map map = new HashMap();
		LoanUser user1 = service.seluser(user);
		if (user1 != null) {  //用户名或号码存在时
			map.put("msg", 1);
		} else {
			map.put("msg", 0);
		}
		return map;
	}

	//添加用户
	@RequestMapping("/add")
	public String add(LoanUser user, Model model) {
		System.out.println("loanUser 信息 ：" + user.toString());
		user.setAutonym(LoanConstants.Autonym_State_no);//修改实名状态
		user.setAssessstate(LoanConstants.Evaluation_Of_State_into);
		service.addLuser(user);
		model.addAttribute("name", user.getName());
		return "loan/reglog/loanregister1";
	}

	//登录验证
	@RequestMapping("/loanyanzLogin")
	@ResponseBody
	public Boolean login_verification(String phoneemail, String password, HttpSession session) {
		LoanUser user = service.loanpemail(phoneemail, password);
		if (user == null) {
			return false;
		} else {
			session.setAttribute("loanuser", user);
			return true;
		}
	}

	//验证短信验证码
	@RequestMapping("/yzdxyzm")
	@ResponseBody
	public int yzdxyzm(int code) {
		if (code != codes) {
			return 1;
		}
		return 0;
	}


	//生成短信验证码
	@RequestMapping("/dxyzm")
	@ResponseBody
	public int dxyzm(LoanUser user) {
		LoanUser user1 = service.seluser(user);  //验证手机是否注册
		if (user1 != null) {
			return 1;
		}
		Random random = new Random();
//        int i = random.nextInt(999999);
		int i = 000000;
		codes = i;
//        VerificationCode vc = new VerificationCode();
//        vc.vercode(user.getPhone(), i);
		return 0;
	}

	/**
	 * 退出登录
	 *
	 * @param session
	 * @return
	 */
	@GetMapping("/exit")
	public String exit(HttpSession session) {
		session.invalidate();
		return "redirect:/loan/reglog/tologin";
	}

}
